import { defineStore } from 'pinia'
import axios from 'axios'
import { nanoid } from 'nanoid'
import {reactive} from 'vue'

// export const useTalksStore = defineStore('talk', {
//     actions: {
//         async getTalk() {
//             const { data: { content: title } } = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
//             const obj = { id: nanoid(), title }
//             this.talks.unshift(obj)
//         }
//     },
//     state() {
//         return {
//             talks:JSON.parse(localStorage.getItem('talks') as string) || []
//         }
//     }
// })


export const useTalksStore = defineStore('talk', () => {
    
    const talks = reactive(JSON.parse(localStorage.getItem('talks') as string) || [])
    
    async function getTalk() {
        const { data: { content: title } } = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
        const obj = { id: nanoid(), title }
        talks.unshift(obj)
    }

    return {talks,getTalk}

})